﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using Microsoft.CSharp.RuntimeBinder;

namespace YArchitech.Temp
{
	internal partial class OptionForm : System.Windows.Forms.Form
	{
		public OptionForm()
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			for (int i = 1; i < 100; i++)
			{
				this.hyComboBoxReturnT.AddItem(i.ToString());
				this.hyComboBoxSupplyT.AddItem(i.ToString());
			}
		}

		public OptionForm(HYPipeSystemOption option) : this()
		{
			this.SetOption(option);
		}

		public bool SetOption(HYPipeSystemOption option)
		{
			this.m_option = option;
			return this.Init();
		}

		private void SetValue(YJKTextBox tb, double dValue)
		{
			string format = "G";
			tb.Text = dValue.ToString(format);
			tb.Tag = dValue;
		}

		private void SetValue(YJKTextBox tb, int nValue)
		{
			tb.Text = nValue.ToString();
			tb.Tag = nValue;
		}

		private void SetValue(YJKComboBox cmb, dynamic nValue)
		{
            //if (OptionForm.<>o__11.<>p__1 == null)
            //{
            //    OptionForm.<>o__11.<>p__1 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(OptionForm)));
            //}
            //Func<CallSite, object, string> target = OptionForm.<>o__11.<>p__1.Target;
            //CallSite <>p__ = OptionForm.<>o__11.<>p__1;
            //if (OptionForm.<>o__11.<>p__0 == null)
            //{
            //    OptionForm.<>o__11.<>p__0 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(OptionForm), new CSharpArgumentInfo[]
            //    {
            //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
            //    }));
            //}
            //cmb.Text = target(<>p__, OptionForm.<>o__11.<>p__0.Target(OptionForm.<>o__11.<>p__0, nValue));
		}

		protected bool Init()
		{
			this.SetValue(this.hyTextBoxDensity, this.m_option.Density);
			this.SetValue(this.tbViscosity, this.m_option.Viscosity * 1000000.0);
			this.SetValue(this.tbTerminalLoss, this.m_option.TerminalLoss / 1000.0);
			this.SetValue(this.hyComboBoxSupplyT, this.m_option.SupplyTemperature);
			this.SetValue(this.hyComboBoxReturnT, this.m_option.ReturnTemperature);
			this.SetValue(this.tbFrictionalFix, this.m_option.FrictionFix);
			this.SetValue(this.tbShockFix, this.m_option.ShockFix);
			this.SetValue(this.hyTextBoxMinDN, this.m_option.DNRange.Min);
			this.SetValue(this.hyTextBoxMaxDN, this.m_option.DNRange.Max);
			return true;
		}

		private void OptionForm_Load(object sender, EventArgs e)
		{
			base.CenterToParent();
		}

		private void GetValue(YJKTextBox textBox, ref double dValue)
		{
			double num = dValue;
			if (double.TryParse(textBox.Text, out num))
			{
				dValue = num;
			}
		}

		private void GetValue(YJKTextBox textBox, ref int nValue)
		{
			int num = nValue;
			if (int.TryParse(textBox.Text, out num))
			{
				nValue = num;
			}
		}

		private void GetValue(YJKComboBox comBo, ref int nValue)
		{
			int num = nValue;
			if (int.TryParse(comBo.Text, out num))
			{
				nValue = num;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			double num = this.m_option.Density;
			this.GetValue(this.hyTextBoxDensity, ref num);
			this.m_option.Density = num;
			num = this.m_option.Viscosity * 1000000.0;
			this.GetValue(this.tbViscosity, ref num);
			this.m_option.Viscosity = num / 1000000.0;
			num = this.m_option.TerminalLoss / 1000.0;
			this.GetValue(this.tbTerminalLoss, ref num);
			this.m_option.TerminalLoss = num * 1000.0;
			num = this.m_option.FrictionFix;
			this.GetValue(this.tbFrictionalFix, ref num);
			this.m_option.FrictionFix = num;
			num = this.m_option.ShockFix;
			this.GetValue(this.tbShockFix, ref num);
			this.m_option.ShockFix = num;
			int supplyTemperature = this.m_option.SupplyTemperature;
			int returnTemperature = this.m_option.ReturnTemperature;
			this.GetValue(this.hyComboBoxSupplyT, ref supplyTemperature);
			this.m_option.SupplyTemperature = supplyTemperature;
			this.GetValue(this.hyComboBoxReturnT, ref returnTemperature);
			this.m_option.ReturnTemperature = returnTemperature;
			double min = this.m_option.DNRange.Min;
			this.GetValue(this.hyTextBoxMinDN, ref min);
			double max = this.m_option.DNRange.Max;
			this.GetValue(this.hyTextBoxMaxDN, ref max);
			this.m_option.DNRange.Set(min, max);
			this.m_option.Save(Product.UserConfigFile);
		}

		private const string Type = "Type";

		private const string MainVelocity = "Main";

		private const string SideVelocity = "Side";

		private const string SideSideVelocity = "SideSide";

		private const string TerminalVelocity = "Terminal";

		private HYPipeSystemOption m_option;
	}
}
